# -*- autoconf -*-
#########################################
##
# Miscellaneous Operating-System characteristics
##
#########################################


#	Determine how to test for character devices
#	[We assume that /dev/null does actually exist!]
#
if test -c /dev/null; then
    CDEV_TEST_FLAG="-c"
elif test -f /dev/null; then
    CDEV_TEST_FLAG="-f"
else
    AC_MSG_WARN([Can't reliably detect character devices])
    # Is it actually sensible to fall back on "test -f" ?
    CDEV_TEST_FLAG="-f"
fi

#       Kernel Location
#	    used in library/agent
#
AC_CACHE_CHECK([for location of system kernel],
    ac_cv_KERNEL_LOC,
   [ac_cv_KERNEL_LOC="unknown"
    for i in /vmunix /hp-ux /stand/vmunix /dev/ksyms /kernel/unix /kernel/genunix /netbsd /unix /kernel /bsd /mach_kernel /boot/kernel/kernel
    do
        if test $CDEV_TEST_FLAG $i; then
            ac_cv_KERNEL_LOC="$i"
            break;
        fi
    done
    #
    # In principle, this test is meant to provide a default path to the kernel
    # In practice, the test is broken and the kernel path is set to "unknown"
    #
    # Note that on certain systems (e.g. some Linux distributions),
    #    it is impossible to predict the name of the kernel,
    #    particularly if it includes the kernel version, architecture, etc
    #
    # Since this default will never actually be used, then don't try to set it
    #
    if test $i = "unknown"; then
        AC_MSG_WARN([Can't find system kernel... hopefully this won't be needed!])
    fi
])
AC_DEFINE_UNQUOTED(KERNEL_LOC,"$ac_cv_KERNEL_LOC", [location of UNIX kernel])


#       Swap device Location
#           (/dev/dmem or /dev/drum)
#	    used in agent only
#
AC_CACHE_CHECK([for location of swap device],
    ac_cv_DMEM_LOC,
   [if test $CDEV_TEST_FLAG /dev/dmem; then
        ac_cv_DMEM_LOC="/dev/dmem"
    elif test $CDEV_TEST_FLAG /dev/drum; then
        ac_cv_DMEM_LOC="/dev/drum"
    else
        ac_cv_DMEM_LOC="none"
    fi
])
#
#  Note that it's OK if this device is not found
#
if test "x$ac_cv_DMEM_LOC" != "xnone"; then
    AC_DEFINE_UNQUOTED(DMEM_LOC,"$ac_cv_DMEM_LOC", [location of swap device])
fi


#       Mount table Location
#	    used in agent only
#
AC_CACHE_CHECK([for mount table location],
    ac_cv_ETC_MNTTAB,
   [ac_cv_ETC_MNTTAB="${with_mnttab:-unknown}"
    if test "$ac_cv_ETC_MNTTAB" = "unknown"; then
        for i in /etc/mnttab /etc/mtab /etc/filesystems /dev/mnttab
        do
            if test -f $i -o -c $i; then
                ac_cv_ETC_MNTTAB="$i"
                break;
            fi
        done
    fi
])
AC_DEFINE_UNQUOTED(ETC_MNTTAB,"$ac_cv_ETC_MNTTAB", [location of mount table list])


#       Printing
#	    used in agent only
#
AC_PATH_PROG([LPSTAT_PATH],lpstat)
if test x$LPSTAT_PATH != x; then
    AC_DEFINE_UNQUOTED(LPSTAT_PATH, "$LPSTAT_PATH",
	[Path to the lpstat command])
    AC_DEFINE(HAVE_LPSTAT, 1, [Set if the lpstat command is available])
fi
if test -r /etc/printcap; then
    AC_DEFINE(HAVE_PRINTCAP, 1, [Set if /etc/printcap exists])
fi


#       Check ps args
#	    used in agent only
#
AC_CACHE_CHECK([for correct flags to ps],
     ac_cv_ps_flags,
     [
case "x$PARTIALTARGETOS" in
    xcygwin|xmingw32*)
	ac_cv_ps_flags="-e";;
    *)
	ac_cv_ps_flags=""
	for args in -e -el acx -acx "-o pid,tt,state,time,ucomm" ax; do
	    if test "`($PSPROG $args 2>&1) | $EGREP ' (ps) *$' | awk '{print $NF}'`" = "ps"; then
		ac_cv_ps_flags=$args
		break
	    fi
	done
	if test "x${ac_cv_ps_flags}" = x; then
	    AC_MSG_WARN([Unable to determine valid ps flags...  defaulting...])
	    ac_cv_ps_flags="-acx"
	elif $PSPROG $ac_cv_ps_flags -J 0 >/dev/null 2>&1; then
	    ac_cv_ps_flags="$ac_cv_ps_flags -J 0"
	fi
	;;
esac
])

PSCMD="$PSPROG $ac_cv_ps_flags"
AC_SUBST(PSCMD)
AC_DEFINE_UNQUOTED(PSCMD, "$PSPROG $ac_cv_ps_flags",
  [Command to generate ps output, the final column must be the process
   name withOUT arguments])


##
#   System/Compilation-related
##

#       Test for SIGHUP
#

AC_MSG_CHECKING([for SIGHUP])
AC_COMPILE_IFELSE(
    [AC_LANG_PROGRAM([#include <signal.h>], [return SIGHUP])],
    [netsnmp_have_sighup=yes],
    [netsnmp_have_sighup=no]
)
AC_MSG_RESULT([$netsnmp_have_sighup])
if test "x$netsnmp_have_sighup" = xyes; then
    AC_DEFINE([HAVE_SIGHUP], [1],
              [Define if SIGHUP is defined in <signal.h>.])
fi


#       Check whether sysctl() is usable
#	    used in library/agent
#
if test $cross_compiling = yes; then
    AC_MSG_WARN([Can't check sysctl, manually define NETSNMP_CAN_USE_SYSCTL if platform support available])
else
    AC_CACHE_CHECK([if sysctl can read kernel information],
        ac_cv_NETSNMP_CAN_USE_SYSCTL,
       [AC_RUN_IFELSE([AC_LANG_SOURCE([[
#ifdef TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# ifdef HAVE_SYS_TIME_H
#  include <sys/time.h>
# else
#  include <time.h>
# endif
#endif
#ifdef HAVE_SYS_PARAM_H
# include <sys/param.h>
#endif
#include <sys/types.h>
#ifdef HAVE_SYS_SYSCTL_H
# include <sys/sysctl.h>
#endif
#include <stddef.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
  int                 mib[2];
  size_t              len;
  struct timeval boottime;
  
  mib[0] = CTL_KERN;
  mib[1] = KERN_BOOTTIME;
  
  len = sizeof(boottime);
  return 1 - (sysctl(mib, 2, &boottime, &len, NULL, 0) == 0 && boottime.tv_sec);
}
        ]])],[ac_cv_NETSNMP_CAN_USE_SYSCTL=yes],[ac_cv_NETSNMP_CAN_USE_SYSCTL=no],[ac_cv_NETSNMP_CAN_USE_SYSCTL=no])])
fi

if test "x$ac_cv_NETSNMP_CAN_USE_SYSCTL" = "xyes"; then
  AC_DEFINE(NETSNMP_CAN_USE_SYSCTL, 1, [sysctl works to get boottime, etc...])
fi


#   Check whether TCP timer constants are indeed constant
#       or depend on the kernel clock tick 'hz'.        (FreeBSD)
#
#       If the latter, then we will need to have a local
#       variable 'hz' defined and with a suitable value,
#       whenever we want to  use one one of these 'constants'.
#
AC_CACHE_CHECK([whether TCP timers depend on 'hz'],
    ac_cv_TCPTV_NEEDS_HZ,
   [AC_EGREP_CPP([\<hz\>],
        [
#include <netinet/tcp_timer.h>
TCPTV_MIN
TCPTV_REXMTMAX
        ],
        ac_cv_TCPTV_NEEDS_HZ=yes,
        ac_cv_TCPTV_NEEDS_HZ=no)])

if test "x$ac_cv_TCPTV_NEEDS_HZ" = "xyes"; then
    AC_DEFINE(TCPTV_NEEDS_HZ, 1,
        [Define if the TCP timer constants in <netinet/tcp_timer.h>
         depend on the integer variable 'hz'.  @<:@FreeBSD@:>@])
fi


#   Check whether IP Route table is cached              (Solaris)
#	    used in agent only
#
case $target_os in
  solaris*)
    #    Header checks                                  (Solaris 2.6/7)
    #
    AC_CHECK_HEADERS(inet/common.h)
    AC_CHECK_HEADERS(inet/ip.h,ac_inet_ip_h=yes,ac_inet_ip_h=no,
        AC_INCLUDES_DEFAULT([])
        [
#ifdef HAVE_SYS_STREAM_H
#include <sys/stream.h>
#endif
#ifdef HAVE_INET_COMMON_H
#include <inet/common.h>
#endif
        ])
    if test $ac_inet_ip_h = yes ; then
        AC_CACHE_CHECK([for mib2_ipRouteEntry_t in inet/mib2.h],
            ac_cv_mib2_ipRouteEntry_t,
           [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
#ifdef HAVE_INET_MIB2_H
#include <inet/mib2.h>
#endif
               ]], [[
  mib2_ipRouteEntry_t testit;
  if(sizeof(testit))
    return(0);
               ]])],[ac_cv_mib2_ipRouteEntry_t=yes],[ac_cv_mib2_ipRouteEntry_t=no])])

        if test "x$ac_cv_mib2_ipRouteEntry_t" = "xyes" ; then
            AC_CACHE_CHECK([whether IRE_CACHE is defined in inet/ip.h],
                ac_cv_ire_cache,
               [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
#ifdef HAVE_INET_IP_H
#include <inet/ip.h>
#endif
                   ]], [[
  int testit=IRE_CACHE;
  if(testit)
    return(0);
                   ]])],[ac_cv_ire_cache=yes],[ac_cv_ire_cache=no])])
        else
            ac_cv_ire_cache=no
        fi

        if test "x$ac_cv_ire_cache" = "xyes" ; then
            AC_DEFINE(HAVE_DEFINED_IRE_CACHE,[],
                [define to 1 if you have IRE_CACHE defined in <inet/ip.h> header file.])
        else
            AC_MSG_WARN([It seems that you have inet/ip.h, but IRE_CACHE is not defined. \
That means that your query to ipRoute MIB could generate over 20k results! \
And this can be very slow.])
        fi
    fi         # "test $ac_inet_ip_h = yes"
    ;;
esac

#   Check whether SO_BINDTODEVICE is available.
#

AC_MSG_CHECKING([for SO_BINDTODEVICE])
AC_LINK_IFELSE([AC_LANG_PROGRAM([
#include <sys/types.h>
#include <sys/socket.h>
], [
return SO_BINDTODEVICE
])],
[AC_DEFINE([HAVE_SO_BINDTODEVICE], [1],
  [Define to 1 if SO_BINDTODEVICE is available])
 AC_MSG_RESULT([yes])],
[AC_MSG_RESULT([no])])

#   Check whether struct in_pktinfo.ipi_spec_dst is available.
#

AC_CHECK_MEMBERS([struct in_pktinfo.ipi_spec_dst], [], [], [
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
])

#   Check whether IP_PKTINFO is usable.
#

AC_CACHE_CHECK([for IP_PKTINFO ],
                [ac_cv_have_ip_pktinfo],
[AC_LINK_IFELSE([AC_LANG_PROGRAM([
#include <stdio.h>  /* printf() */
#include <stdlib.h> /* malloc() */
#include <string.h> /* memset() */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h> /* inet_ntoa() */
], [
    void *buf;
    int len;
    void *from;
    socklen_t *fromlen;

    struct iovec iov;
    char *cmsg = malloc(CMSG_SPACE(sizeof(struct in_pktinfo)));
    struct cmsghdr *cm;
    struct msghdr msg;

    iov.iov_base = buf;
    iov.iov_len = len;

    memset(&msg, 0, sizeof msg);
    msg.msg_name = from;
    msg.msg_namelen = *fromlen;
    msg.msg_iov = &iov;
    msg.msg_iovlen = 1;
    msg.msg_control = &cmsg;
    msg.msg_controllen = sizeof(cmsg);

    for (cm = CMSG_FIRSTHDR(&msg); cm; cm = CMSG_NXTHDR(&msg, cm)) {
        if (cm->cmsg_level == SOL_IP && cm->cmsg_type == IP_PKTINFO) {
            struct in_pktinfo* src = (struct in_pktinfo *)CMSG_DATA(cm);
            printf("Address: %s; index: %d\n", inet_ntoa(src->ipi_addr),
	           src->ipi_ifindex);
        }
    }
])],
[ac_cv_have_ip_pktinfo=yes],
[ac_cv_have_ip_pktinfo=no]
)])
if test x$ac_cv_have_ip_pktinfo = xyes; then
    AC_DEFINE(HAVE_IP_PKTINFO, 1, [Set if IP_PKTINFO is usable])
fi

#   Check whether IPV6_RECVPKTINFO is usable.
#

AC_CACHE_CHECK([for IPV6_RECVPKTINFO ],
                [ac_cv_have_ipv6_recvpktinfo],
[AC_LINK_IFELSE([AC_LANG_PROGRAM([
#include <netinet/in.h>
], [
return IPV6_RECVPKTINFO
])],
[ac_cv_have_ipv6_recvpktinfo=yes],
[ac_cv_have_ipv6_recvpktinfo=no]
)])
if test x$ac_cv_have_ipv6_recvpktinfo = xyes; then
    AC_DEFINE(HAVE_IPV6_RECVPKTINFO, 1, [Set if IPV6_RECVPKTINFO is usable])
fi

#   Check whether IP_RECVDSTADDR is usable.
#

AC_CACHE_CHECK([for IP_RECVDSTADDR ],
                [ac_cv_have_ip_recvdstaddr],
[AC_LINK_IFELSE([AC_LANG_PROGRAM([
#include <stdio.h>  /* printf() */
#include <stdlib.h> /* malloc() */
#include <string.h> /* memset() */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h> /* inet_ntoa() */
], [
    void *buf;
    int len;
    void *from;
    socklen_t *fromlen;

    struct iovec iov;
    char *cmsg = malloc(CMSG_SPACE(sizeof(struct in_addr)));
    struct cmsghdr *cm;
    struct msghdr msg;

    iov.iov_base = buf;
    iov.iov_len = len;

    memset(&msg, 0, sizeof msg);
    msg.msg_name = from;
    msg.msg_namelen = *fromlen;
    msg.msg_iov = &iov;
    msg.msg_iovlen = 1;
    msg.msg_control = &cmsg;
    msg.msg_controllen = sizeof(cmsg);

    for (cm = CMSG_FIRSTHDR(&msg); cm; cm = CMSG_NXTHDR(&msg, cm)) {
        if (cm->cmsg_level == IPPROTO_IP && cm->cmsg_type == IP_RECVDSTADDR) {
            struct in_addr* src = (struct in_addr *)CMSG_DATA(cm);
            printf("Address: %s\n", inet_ntoa(src));
        }
    }
])],
[ac_cv_have_ip_recvdstaddr=yes],
[ac_cv_have_ip_recvdstaddr=no]
)])
if test x$ac_cv_have_ip_recvdstaddr = xyes; then
    AC_DEFINE(HAVE_IP_RECVDSTADDR, 1, [Set if IP_RECVDSTADDR is usable])
fi
